package com.ssy.lingxi.logistics.serviceimpl;

import com.ssy.lingxi.logistics.api.dto.request.LogisticsCompanyAddRequest;
import com.ssy.lingxi.logistics.api.enums.CooperateTypeEnum;
import com.ssy.lingxi.logistics.entity.Company;
import com.ssy.lingxi.logistics.model.enums.CommonStatusEnum;
import com.ssy.lingxi.logistics.repository.BaseLogisticsCompanyRepository;
import com.ssy.lingxi.logistics.repository.CompanyRepository;
import com.ssy.lingxi.logistics.service.ILogisticsCompanyFeignService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 初始化基础物流公司接口实现
 * @author wyx
 * @version 2.0.0
 * @date 2021-12-14
 */
@Service
public class LogisticsCompanyFeignServiceImpl implements ILogisticsCompanyFeignService {
    @Resource
    private CompanyRepository companyRepository;
    @Resource
    private BaseLogisticsCompanyRepository baseLogisticsCompanyRepository;


    /**
     * 添加基础物流公司
     * @param request 接口参数
     * @return 返回结果
     */
    @Async
    @Override
    public void addCompany(LogisticsCompanyAddRequest request) {
        List<Company> companyList = baseLogisticsCompanyRepository.findAll()
                .stream().map(baseCompany -> {
                    Company company = new Company();
                    company.setCode(baseCompany.getCode());
                    company.setName(baseCompany.getName());
                    company.setMemberId(request.getMemberId());
                    company.setRoleId(request.getRoleId());
                    company.setStatus(CommonStatusEnum.ENABLE.getCode());
                    company.setCooperateType(CooperateTypeEnum.MERCHANT.getCode());
                    company.setSort(baseCompany.getSort());
                    company.setCreateTime(System.currentTimeMillis());
                    return company;
                }).collect(Collectors.toList());

        companyRepository.saveAll(companyList);
    }
}
